Skip to content

main:cpp: Fix thread race #5

Merged
merged 1 commit into from
Oct 9, 2017
Merged

main:cpp: Fix thread race #5

merged 1 commit into from
Oct 9, 2017

Conversation

donald
Copy link
Member

@donald donald commented Oct 8, 2017

There is some a global variable read_time which is updated by
all threads without proper synchronization.

WARNING: ThreadSanitizer: data race (pid=15896)
  Read of size 4 at 0x00000066d53c by thread T7:
    #0 t_PairAlign(void*) /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:118 (bsmap+0x00000042c4f2)

  Previous write of size 4 at 0x00000066d53c by thread T6:
    [failed to restore the stack]

  Location is global 'read_time' of size 4 at 0x00000066d53c (bsmap+0x00000066d53c)

  Thread T7 (tid=15960, running) created by main thread at:
    #0 pthread_create /scratch/local/bee-root/gcc/gcc-5.3.0-0/source/libsanitizer/tsan/tsan_interceptors.cc:895 (libtsan.so.0+0x000000026c44)
    #1 Do_PairAlign() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:128 (bsmap+0x00000042c613)
    #2 RunProcess() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:499 (bsmap+0x0000004306b0)
    #3 main /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:611 (bsmap+0x0000004320ae)

  Thread T6 (tid=15959, finished) created by main thread at:
    #0 pthread_create /scratch/local/bee-root/gcc/gcc-5.3.0-0/source/libsanitizer/tsan/tsan_interceptors.cc:895 (libtsan.so.0+0x000000026c44)
    #1 Do_PairAlign() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:128 (bsmap+0x00000042c613)
    #2 RunProcess() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:499 (bsmap+0x0000004306b0)
    #3 main /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:611 (bsmap+0x0000004320ae)

SUMMARY: ThreadSanitizer: data race /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:118 t_PairAlign(void*)
==================

This is not a real issue, because the value is not currently
used. But because we are holding the mutex_fout mutex at this point
in code anyway, swap the order and work in the global first, then
release the mutex. This puts read_time under the protection of
mutex_fout.

There is some a global variable read_time which is updated by
all threads without proper synchronization.

    WARNING: ThreadSanitizer: data race (pid=15896)
      Read of size 4 at 0x00000066d53c by thread T7:
        #0 t_PairAlign(void*) /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:118 (bsmap+0x00000042c4f2)

      Previous write of size 4 at 0x00000066d53c by thread T6:
        [failed to restore the stack]

      Location is global 'read_time' of size 4 at 0x00000066d53c (bsmap+0x00000066d53c)

      Thread T7 (tid=15960, running) created by main thread at:
        #0 pthread_create /scratch/local/bee-root/gcc/gcc-5.3.0-0/source/libsanitizer/tsan/tsan_interceptors.cc:895 (libtsan.so.0+0x000000026c44)
        #1 Do_PairAlign() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:128 (bsmap+0x00000042c613)
        #2 RunProcess() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:499 (bsmap+0x0000004306b0)
        #3 main /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:611 (bsmap+0x0000004320ae)

      Thread T6 (tid=15959, finished) created by main thread at:
        #0 pthread_create /scratch/local/bee-root/gcc/gcc-5.3.0-0/source/libsanitizer/tsan/tsan_interceptors.cc:895 (libtsan.so.0+0x000000026c44)
        #1 Do_PairAlign() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:128 (bsmap+0x00000042c613)
        #2 RunProcess() /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:499 (bsmap+0x0000004306b0)
        #3 main /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:611 (bsmap+0x0000004320ae)

    SUMMARY: ThreadSanitizer: data race /scratch/cluster/buczek/bsmap/bsmap.git/main.cpp:118 t_PairAlign(void*)
    ==================

This is not a real issue, because the value is not currently
used. But because we are holding the mutex_fout mutex at this point
in code anyway, swap the order and work in the global first, then
release the mutex. This puts read_time under the protection of
mutex_fout.
@pmenzel pmenzel merged commit b782cd3 into master Oct 9, 2017
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants